package cn.hellochaos.generator.service.impl;

import cn.hellochaos.generator.entity.CaipuFuliao;
import cn.hellochaos.generator.entity.CaipuList;
import cn.hellochaos.generator.entity.CaipuSummary;
import cn.hellochaos.generator.mapper.CaipuFuliaoMapper;
import cn.hellochaos.generator.service.CaipuFuliaoService;
import cn.hellochaos.generator.utils.CheckObjectIsNullUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import cn.hellochaos.generator.exception.bizException.BizException;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 辅料 服务实现类
 * </p>
 *
 * @author test
 * @since 2021-01-23
 */
@Slf4j
@Service
public class CaipuFuliaoServiceImpl extends ServiceImpl<CaipuFuliaoMapper, CaipuFuliao> implements CaipuFuliaoService {

    @Autowired
    CaipuFuliaoMapper caipuFuliaoMapper;

    @Override
    public List<CaipuFuliao> getCaipuBuzhouByCookBookId(int cookbookId) {
        QueryWrapper<CaipuFuliao> queryWrapper = new QueryWrapper<CaipuFuliao>();
        queryWrapper.eq("id_cookbook",cookbookId);
        List<CaipuFuliao> caipuFuliaos = caipuFuliaoMapper.selectList(queryWrapper);
        if (CheckObjectIsNullUtils.objCheckIsNull(caipuFuliaos)){
            return new ArrayList<CaipuFuliao>();
        }
        return caipuFuliaos;
    }

    @Override
    public Page<CaipuFuliao> listCaipuFuliaosByPage(int page, int pageSize, String factor) {
        log.info("正在执行分页查询caipuFuliao: page = {} pageSize = {} factor = {}", page, pageSize, factor);
        QueryWrapper<CaipuFuliao> queryWrapper = new QueryWrapper<CaipuFuliao>().like("", factor);
        //TODO 这里需要自定义用于匹配的字段,并把wrapper传入下面的page方法
        Page<CaipuFuliao> result = super.page(new Page<>(page, pageSize));
        log.info("分页查询caipuFuliao完毕: 结果数 = {} ", result.getRecords().size());
        return result;
    }

    @Override
    public CaipuFuliao getCaipuFuliaoById(int id) {
        log.info("正在查询caipuFuliao中id为{}的数据", id);
        CaipuFuliao caipuFuliao = super.getById(id);
        log.info("查询id为{}的caipuFuliao{}", id, (null == caipuFuliao ? "无结果" : "成功"));
        return caipuFuliao;
    }

    @Override
    public int insertCaipuFuliao(CaipuFuliao caipuFuliao) {
        log.info("正在插入caipuFuliao");
        if (super.save(caipuFuliao)) {
            log.info("插入caipuFuliao成功,id为{}", caipuFuliao.getCaipuFuliaoId());
            return caipuFuliao.getCaipuFuliaoId();
        } else {
            log.error("插入caipuFuliao失败");
            throw new BizException("添加失败");
        }
    }

    @Override
    public int deleteCaipuFuliaoById(int id) {
        log.info("正在删除id为{}的caipuFuliao", id);
        if (super.removeById(id)) {
            log.info("删除id为{}的caipuFuliao成功", id);
            return id;
        } else {
            log.error("删除id为{}的caipuFuliao失败", id);
            throw new BizException("删除失败[id=" + id + "]");
        }
    }

    @Override
    public int updateCaipuFuliao(CaipuFuliao caipuFuliao) {
        log.info("正在更新id为{}的caipuFuliao", caipuFuliao.getCaipuFuliaoId());
        if (super.updateById(caipuFuliao)) {
            log.info("更新d为{}的caipuFuliao成功", caipuFuliao.getCaipuFuliaoId());
            return caipuFuliao.getCaipuFuliaoId();
        } else {
            log.error("更新id为{}的caipuFuliao失败", caipuFuliao.getCaipuFuliaoId());
            throw new BizException("更新失败[id=" + caipuFuliao.getCaipuFuliaoId() + "]");
        }
    }

}
